--  NewTeamSettingController

local NewTeamSettingView = import("..views.NewTeamSettingView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(params)
	self.super.ctor(self, params)
end

function ClassRef:init()
	self._model = app:getInst("TeamModel")
end

function ClassRef:listEvent()
	return {
		"onTeamUpdate",
		"onTeamMemberUpdate",
		"onTeamLeaderChanged",
		"onTeamSettingUpdate",
		"onTeamNewApplyer"
	}
end


function ClassRef:onEvent(eventName, data)
	if eventName == "onTeamUpdate" then -- 整个队伍数据改变
		self:refreshTeamTarget()
	elseif eventName == "onTeamMemberUpdate" then -- 队伍 成员 改变
	elseif eventName == "onTeamLeaderChanged" then -- 队长改变
	elseif eventName == "onTeamSettingUpdate" then -- 队伍设置改变
		self:refreshTeamSetting()
	elseif eventName == "onTeamNewApplyer" then -- 来新申请人
	end
end

function ClassRef:openView(data)
	if self._model:hasTeam() then
		self._model:reqGetTeamInfo() -- 打开UI的时候，主动拉取一次队伍数据
	end

	if cc.isDead(self._view) then
		self._view = NewTeamSettingView.new( app.mvc.VIEWTYPE_BLUR, data )
		self._view:onCloseView(handler(self, self.closeView))
		self._view:onClearView(handler(self, self.clearView))
		self._view:onOperate(handler(self, self._onOperate))
	end
	self:addChild(self._view, data)
	self:refreshTeamTarget()
	self:refreshAutoFollowStatus()
	self:refreshTeamSetting()

	self._view:show()
end
function ClassRef:closeView(data)
	if self._view then
		if self._model:isMyTeam() then
			local temp = self._view:getTeamRecruitInfoEndingModified()
			if temp and not self._model:isTwoRecruitInfoEqual( temp ) then -- 改变了队伍设置
				self._model:C2S_UpdateRecruitInfo( temp )
			end
		end
		self:removeChild(self._view, {isFade=false})
		self:clearView()
	end
end
function ClassRef:clearView(data)
	if self._view then self._view = nil end
end

------------------------------ view logic ---------------------------------
-- 刷新队伍设置
function ClassRef:refreshTeamSetting()
	if not self:isViewAlive() then return end
	self._view:refreshTeamSetting( self._model:getTeam() )
end

-- 刷新队伍目标
function ClassRef:refreshTeamTarget()
	if not self:isViewAlive() then return end

	local teamTargetVo = self._model:getTeamTargetVo()
	self._view:setTarget( teamTargetVo and teamTargetVo:getFullName() or "")
end

-- 刷新跟随状态标记
function ClassRef:refreshAutoFollowStatus()
	if not self:isViewAlive() then return end

	local isLeader = self._model:isLeader()
	local isAutoFollow = self._model:isWithTeam()
	if isLeader then
		self._view:showAutoFollow( false )
		self._view:setAutoFollowBtnClickEnabled( true )
	else
		self._view:showAutoFollow( true )
		self._view:setAutoFollowStatus( isAutoFollow )

		if self._model:isTeamNormal() then -- 队伍状态为正常？
			local mapId = MapManager:getMapId()
			if mapId and mapId == tonumber(S("lionHeartHornMapID")) then -- 所处场景为狮心号角？
				self._view:setAutoFollowBtnClickEnabled( false )
			else
				self._view:setAutoFollowBtnClickEnabled( true )
			end
		else
			self._view:setAutoFollowBtnClickEnabled( false )
		end
	end
end

-- 切换自动跟随状态
function ClassRef:teamMemberChangeAutoFollow()
	if self._model:isTeamNormal() then -- normal
		local mapId = MapManager:getMapId()
		if mapId and mapId == tonumber(S("lionHeartHornMapID")) then -- 所处场景为狮心号角？
			display.pushToast( L("tips_3000401") )
			return
		end
	else
		display.pushToast( L("tips_3000401") )
		return
	end

	local oldFollowStatus = self._model:isWithTeam()
	self._model:reqFollowLeader( not oldFollowStatus )
end

-- 反向打开队伍查找器
function ClassRef:relocateToTeamSearching()
	self:closeView() -- 关闭自己
	app:sendMsg("NewTeamMemberController", "closeView") -- 关闭队伍成员UI

	local teamTargetVo = self._model:getTeamTargetVo()
	local params = teamTargetVo and teamTargetVo:getTeamMatchParams()
	UIHandler:handleMessage({id=Constant.FUNC_TYPE_TEAMMATCHING, mode="open", data = params })
end

function ClassRef:_onOperate( op, ... )
	dump({...}, "NewTeamSettingController.onOperate data")

	local data = ...
	if op == "TeamSetting_relocateToTeamSearching" then
		self:relocateToTeamSearching()
	elseif op == "TeamSetting_changeAutoFollowStatus" then
		self:teamMemberChangeAutoFollow()
	elseif op == "TeamSetting_stopPlay" then
		--停止播放
		app:sendMsg("IMRecordController", "stopPlay")
	elseif op == "TeamSetting_playAudio" then
		--播放录音
		app:sendMsg("IMRecordController", "playAudio", data)
	elseif op == "TeamSetting_loadAudio" then
		--下载录音
		app:sendMsg("IMModel", "downloadAudio", ...)
	end
end

return ClassRef
